package genstream.http

import genstream._
import javax.servlet.http.HttpServletResponse
import javax.validation.constraints.NotNull
import scaladuct.{Extensible, Ref, Starter}

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 16.4.11
 * Time: 12:51
 * To change this template use File | Settings | File Templates.
 */

class HttpResponseResource(response: HttpServletResponse) extends OutputResource {

  def openFragmentedOutputStream(context: StreamingContext): FragmentedOutputStream = {
    new DefaultFragmentedOutputStream(response.getOutputStream)
  }

}

@serializable
@SerialVersionUID(1L)
class HttpResponseResourceStarterCookie {
  @Ref(name = "?") @NotNull
  var responseGetter: () => HttpServletResponse = null
}

@SerialVersionUID(1L)
class HttpResponseResourceStarter extends Starter[ServiceFactory[HttpResponseResource]] {

  def newInstance(): ServiceFactory[HttpResponseResource] = phaseGetResponse(new HttpResponseResourceStarterCookie)

  def phaseGetResponse(cookie: HttpResponseResourceStarterCookie) =
    new FunctionalServiceFactory(new HttpResponseResource(cookie.responseGetter()))

  def destroy(instance: ServiceFactory[HttpResponseResource]) = null

}
